'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var copyRecordModel = require('../../../models/copyRecordModel');
var copyData = require('../../../libs/war/copyData');
var atkData = require('../../../libs/war/atkData');
var copyConf = require('../../../config/copyConf');
var PlayerManager = require('../../../libs/playerManager');
var taskService = require('../../../services/taskService');
var roleService = require('../../../services/roleService');
var blackMarketConf = require('../../../config/blackMarketConf.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 进入副本协议
 * 要给客户端下发的数据:
 * 1.敌方的建筑
 * 2.敌方的英雄,小兵
 * 3.我方的英雄,小兵
 */
Handler.prototype.enter = function (msg, session, next) {
    var role_id = session.uid;
    var copy_id = msg.copy_id;
    if (!copy_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var atk_data;
    var def_data;
    var record_id;
    var war_time;
    var play_story = 0;
    var role_model;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'copy', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            // jshint maxstatements:100
            function (cb) {
                role_model = player_manager.role;
                var copy_model = player_manager.copy;

                if (!copy_model.getCopy(copy_id)) {
                    play_story = 1;
                }

                // 判断这个副本的前置副本是否已经通关
                var copy_conf = copyConf.getCopyConf(copy_id);
                if (!copy_conf) {
                    return next(null, {
                        code: Code.COPY_NOT_EXIST
                    });
                }
                war_time = copy_conf.war_time;

                if (copy_conf.difficulty == 2) {
                    // 判断前置副本是否通关
                    if (!!copy_conf.need_carbon) {
                        var need_carbon = copy_model.getCopy(copy_conf.need_carbon);
                        if (!need_carbon || need_carbon.star <= 0) {
                            return next(null, {
                                code: Code.COPY_NEED_PASS_ERROR
                            });
                        }
                    }
                }

                // 判断体力
                if (role_model.getEnergy() < copy_conf.sp_cost) {
                    return next(null, {
                        code: Code.USER_SP_NOT_ENOUGH
                    });
                }

                // 判断精英副本次数
                if (copy_model.getHardCarbonTimes(copy_id) >= copy_model.getMaxHardCarbonTimes()) {
                    return next(null, {
                        code: Code.COPY_HARD_FIGHT_TIMES_EXCEEDED
                    });
                }

                // 敌方数据
                def_data = _getDef(copy_id);
                if (!def_data) {
                    return next(null, {
                        code: Code.COPY_NOT_EXIST
                    });
                }

                // 副本累计星级统计
                var copy_list = copy_model.copy_list;
                var total_copy_star = 0;
                _.each(copy_list, function (copy_item) {
                    total_copy_star += copy_item.star;
                });
                taskService.addCopyStarTotal(player_manager, total_copy_star);

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;
                var job_type = role_model.getJob();
                copyRecordModel.addRecord(role_id, job_type, copy_id, atk_data, def_data, cb);
            },
            function (model, cb) {
                record_id = model.getRecordId();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    record_id: record_id,
                    war_time: war_time,
                    copy_id: copy_id,
                    atk: atk_data,
                    def: def_data,
                    play_story: play_story
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    atkData.getData(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (copy_id) {
    return copyData.getData(copy_id);
};
